package com.biostime.dingdingbox.common.api;

import java.util.List;


public final class FastResponse {
	
	private int errorCode;
	private int subCode;
	private String errorMsg;
	private String args;
	
	public static FastResponse create(){
		return new FastResponse();
	}
	
	public FastResponse errCode(int errorCode){
		this.errorCode = errorCode;
		return this;
	}
	
	public FastResponse subCode(int subCode){
		this.subCode = subCode;
		return this;
	}
	
	public FastResponse errMsg(String errorMsg){
		this.errorMsg = errorMsg;
		return this;
	}
	
	public FastResponse args(String args){
		this.args = args;
		return this;
	}
	
	private int PageIndex;
    private int PageSize;
    private boolean HasNext;
    private int TotalSize;
    
    public FastResponse pageIndex(int pageIndex){
		this.PageIndex = pageIndex;
		return this;
	}
    
    public FastResponse pageSize(int pageSize){
		this.PageSize = pageSize;
		return this;
	}
    
    public FastResponse total(int total){
		this.TotalSize = total;
		return this;
	}
    
    public FastResponse hasNext(boolean hasNext){
		this.HasNext = hasNext;
		return this;
	}
	
	@SafeVarargs
	public final <T> ListResponse<T> list(List<T>... list){
		Class<?> clazz = null;
		ListResponse<T> res = new ListResponse<T>();
		if(list != null && list.length>0){
			
			if(list[0].size()>0){
				T t =list[0].get(0);
				clazz = t.getClass();
			}else{
				clazz = Object.class;
			}
			
			res.setList(list[0]);
		}
		
		//初始化错误
		res.setError(new ApiError());
		res.getError().setErrCode(this.errorCode);
		res.getError().setSubCode(this.subCode);
		res.getError().setErrMsg(this.errorMsg);
		res.getError().setRequestArgs(this.args);
		
		//初始化属性
		res.setProperty(new ListProperty());
		res.getProperty().setIsList(true);
		res.getProperty().setPageIndex(PageIndex);
		res.getProperty().setPageSize(PageSize);
		res.getProperty().setTotal(TotalSize);
		res.getProperty().setHasNext(HasNext);
		
		/**通过getSimpleName获取的类型名会包含[]符号(如:Customer[])**/
		if(clazz!=null){
			String clazzName = clazz.getSimpleName();
			if(clazzName!=null){
				clazzName = clazzName
						.replace("[", "")
						.replace("]", "");
			}
			res.getProperty().setObjName(clazzName);
		}
		
		return res;
	}
	
	@SafeVarargs
	public final <T> ObjResponse<T> obj(T... obj){
		ObjResponse<T> res = new ObjResponse<T>();
		if(obj != null && obj.length>0){
			res.setObj(obj[0]);
		}
		
		//初始化错误
		res.setError(new ApiError());
		res.getError().setErrCode(this.errorCode);
		res.getError().setSubCode(this.subCode);
		res.getError().setErrMsg(this.errorMsg);
		res.getError().setRequestArgs(this.args);
		
		//初始化属性
		res.setProperty(new ObjProperty());
		res.getProperty().setIsList(false);
		
		/**通过getSimpleName获取的类型名会包含[]符号(如:Customer[])**/
		String clazzName = null;
		if(obj!=null){
			clazzName = obj.getClass().getSimpleName();
			if(clazzName!=null){
				clazzName = clazzName
						.replace("[", "")
						.replace("]", "");
			}
		}else{
			clazzName = Object.class.getSimpleName();
		}
		
		res.getProperty().setObjName(clazzName);
		
		return res;
	}
}
